from __future__ import division
# Qt
from python_qt_binding import loadUi
from python_qt_binding.QtCore import Qt, QTimer, Signal, Slot
from python_qt_binding.QtGui import QImage, QPixmap
from python_qt_binding.QtWidgets import QHeaderView, QMenu, QTreeWidgetItem, QWidget
# ROS
import roslib
import roslib.message
import roslib.names
import rospkg
import rospy
import rostopic
# Others
import os

class MyWidget(QWidget):

    def __init__(self):
        super(MyWidget,self).__init__()
        # read UI file
        rp = rospkg.RosPack()
        ui_file = os.path.join(rp.get_path('rovrqt'), 'resource', 'rov_console.ui')
        loadUi(ui_file, self)
        self.setObjectName('MyPluginUi')
        # connect widget with slot function
        self.pB_lock.clicked.connect(self.open_button_slot)
 
    def open_button_slot(self):
        topic_type, real_topic, fields = rostopic.get_topic_type("/ROV/cmd_vel")        
        data_class = roslib.message.get_message_class(topic_type)
        # 订阅了一个话题
        self.mysub = rospy.Subscriber(real_topic, data_class, self.my_callback)
	
	# 话题的回调函数
    def my_callback(self, msg):
        rospy.loginfo("lock callback")
        pass
	
	# 关闭插件时, 注销订阅的话题
    def close_plugin(self):
        try:
            self.mysub.unregister()
        except AttributeError as e:
            rospy.logerr("Subscriber doesn't open.")
